/*
This softtware use BSD licence (http://opensource.org/licenses/BSD-3-Clause)

Copyright (c) 2013, Martin Lebeda
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the Martin Lebeda nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package cz.lebeda.bas;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author <a href="mailto:martin.lebeda@gmail.com">Martin Lebeda</a>
 *         Date: 10.11.13
 */
public class BackupService {

    final Logger logger = LoggerFactory.getLogger(BackupService.class);

    private String idBackup;
    private Repository repository;
    private List<String> pathToBackupList;
    private List<String> excludes;
    private List<String> excludeDirs;

    private VOConfig cfg = null;
    private VOBackupSumary sumary = new VOBackupSumary();

    /**
     * Create object who provided backup.
     *
     * @param cfg configuration provider
     * @param repository reporitory provider
     */
    public BackupService(VOConfig cfg, Repository repository) {
        this.cfg = cfg;

        this.idBackup = cfg.getBackup();
        this.repository = repository;
        this.pathToBackupList = cfg.getPaths();
        this.excludes = cfg.getExclude();
        this.excludeDirs = cfg.getExcludedir();
    }

    /**
     * Main method to start backup action.
     *
     * @return Index ID of backup (technicaly name of create index file)
     * @throws Exception
     */
    public String run() throws Exception {
        logger.info("Backup started for '{}', to repository '{}'", idBackup, repository.getFullPath());

        Set<VOBackupFile> oldFileSet = repository.getOldBackupIndex(idBackup);
        logger.info("Found {} files in old indexes.", oldFileSet.size());

        Map<VOBackupFile, VOBackupFile> oldFileIndex = new HashMap<>(oldFileSet.size());
        for (VOBackupFile voBackupFile : oldFileSet) {
            oldFileIndex.put(voBackupFile, voBackupFile);
        }
        logger.trace("Index prepared");

        // explore source paths
        List<VOBackupFile> newFileList = exploreBackupFiles();
        sumary.setFounded(newFileList.size());

        logger.trace("finding directories.");
        sumary.setDirs(CollectionUtils.select(newFileList, new Predicate() {
            @Override
            public boolean evaluate(Object object) {
                return (((VOBackupFile) object).isDirectory());
            }
        }).size());
        logger.trace("finding empty files.");
        sumary.setEmptyFiles(CollectionUtils.select(newFileList, new Predicate() {
            @Override
            public boolean evaluate(Object object) {
                return (((VOBackupFile)object).getSize() == 0) && !((VOBackupFile) object).isDirectory();
            }
        }).size());

        logger.trace("start comparing.");
        // find unchanged items
        int cnt = 0;
        for (VOBackupFile voBackupFile : newFileList) {
            if (voBackupFile.unPrepared() && oldFileIndex.containsKey(voBackupFile)) {
                VOBackupFile voBackupFileOld = oldFileIndex.get(voBackupFile);
//                        (VOBackupFile) CollectionUtils.find(oldFileSet, PredicateUtils.equalPredicate(voBackupFile));
                if (voBackupFileOld != null) {
                    voBackupFile.setFileHash(voBackupFileOld.getFileHash());
                    voBackupFile.setCheckSumBck(voBackupFileOld.getCheckSumBck());
                    cnt++;
                } else {
                    logger.trace("Wrog finding old object.");
                }
            }
        }
        sumary.setNonChanged(cnt);
        logger.info("Found {} unchanged files.", cnt);

        
        // for unprepared items do backup
        cnt = 0;
        for (VOBackupFile voBackupFile : newFileList) {
            if (voBackupFile.unPrepared()) {
                try {
                    repository.saveContentToRepo(voBackupFile, oldFileSet);
                    cnt++;
                } catch (Exception e) {
                    sumary.getErrors().add(voBackupFile.getPath() + " - " + e.getMessage());
                    logger.error("Unable to backup "+voBackupFile.getPath() + ": ", e);
                }
            }
        }
        sumary.setProcessed(cnt);
        logger.info("Processed {} changed or new files.", cnt);

        // store index in repository meta
        VOBackupIndex backupIndex = new VOBackupIndex(newFileList);
        backupIndex.setConfig(cfg);
        backupIndex.setSummary(sumary);

        // check before save
        VOCheckIndex voCheckIndex = repository.checkIndex(backupIndex, false, false);
        if (voCheckIndex.allOk()) {
            logger.info("check index: OK");
        } else {
            logger.error("check index: {}", voCheckIndex.toString());
        }
        backupIndex.setCheck(voCheckIndex);

        String indexFileName = repository.saveBackupIndex(backupIndex, idBackup);

        logger.info(sumary.toString());
        logger.info("Backup ended");
        return indexFileName;
    }

    /**
     * Explore path on local system and identify files to backup.
     * It takes into account filters of excluded files and directories.
     *
     * @return list of local files and directories to backup
     */
    private List<VOBackupFile> exploreBackupFiles() {
        List<VOBackupFile> newFileList = new ArrayList<>();
        for (String pathForBackup : pathToBackupList) {
            logger.info("start explore source path '{}' for backup.", pathForBackup);

            // http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/filefilter/WildcardFileFilter.html
            IOFileFilter fileFilter = getWildcardsFilter(excludes);
            IOFileFilter dirFilter = getWildcardsFilter(excludeDirs);

            //noinspection unchecked
            final Collection<File> fileColection =
                    FileUtils.listFilesAndDirs(new File(pathForBackup), fileFilter, dirFilter);

            for (File file : fileColection) {
                newFileList.add(new VOBackupFile(file));
            }

            logger.info("end of explore source path '{}'.", pathForBackup);
        }
        logger.info("end of explore all source paths, found {} items.", newFileList.size());
        return newFileList;
    }

    /**
     * Create filter for finding files. Compile one filter using or operator.
     * <p/>
     * Example: list of texts: *.bak, *.o -> object repsents: *.bak or *.o
     *
     * @param excludes list of user defined wildcard filters
     *
     * @return It takes into account lists
     */
    private IOFileFilter getWildcardsFilter(List<String> excludes) {
        IOFileFilter result;
        if (CollectionUtils.isNotEmpty(excludes)) {
            List<IOFileFilter> fileFilterList = new ArrayList<>(excludes.size());
            for (String exclude : excludes) {
                fileFilterList.add(new WildcardFileFilter(exclude, IOCase.SYSTEM));
            }
            //noinspection SuspiciousToArrayCall
            final IOFileFilter orFiler = FileFilterUtils.or(
                    fileFilterList.toArray(new WildcardFileFilter[excludes.size()]));
            result = FileFilterUtils.notFileFilter(orFiler);
        } else {
            result = FileFilterUtils.trueFileFilter();
        }
        return result;
    }
}
